package net.xdclass.onlinexdclass.config;

import net.xdclass.onlinexdclass.interceptor.CorsInterceptor;
import net.xdclass.onlinexdclass.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器配置
 * 
 * 不用权限可以访问的url   /api/v1/pub
 * 要登录才能访问的url    /api/v1/pri
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    CorsInterceptor corsInterceptor() {
        return new CorsInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //拦截全部(跨域配置) 拦截全部路径，这个跨域需要放在最上面
        registry.addInterceptor(corsInterceptor())
                .addPathPatterns("/**");



        //拦截全部(token验证)
        registry.addInterceptor(loginInterceptor())
                //配置拦截路径
                .addPathPatterns("api/v1/pri/*/*/**")
                .addPathPatterns("/api/v1/pri/**")
                //排除哪些路径不拦截
                .excludePathPatterns("/api/v1/pri/user/login", "/api/v1/pri/user/register");

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
